package com.orderplus.action;

import java.io.IOException;
import java.text.DecimalFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.orderplus.mydrools.DecisionTable;
import com.orderplus.mydrools.Shipping;

/**
 * Servlet implementation class DroolsAction
 */
public class DroolsAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	DecisionTable dt = null;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DroolsAction() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @throws IOException 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getRealPath("/"));
		String jason = "{\"status\":\"0\"}";
		try {
			if(dt == null){
				dt = new DecisionTable();
			}
			String type = request.getParameter("type");
			String weight = request.getParameter("weight");
			String area = request.getParameter("area");
			String [] types = type.split("-");
			DecimalFormat df = new DecimalFormat("0.00");
			if(types.length==1){
				Shipping shipping = dt.runDecision(types[0],weight,area);
				if(shipping.getTotal()>0){
					//jason = "{\"status\":\"1\",\"response\":[{\"type\":\""+shipping.getType()+"\",\"area\":\""+shipping.getArea()+"\",\"total\":\""+df.format(shipping.getTotal())+"\"}]}";
					jason = "{\"status\":\"1\",\"response\":{\"type\":\""+shipping.getType()+"\",\"area\":\""+shipping.getArea()+"\",\"total\":\""+df.format(shipping.getTotal())+"\"}}";
				}
			}else{
				String innerResp = "";
				for(int i=0;i<types.length;i++){
					String innerType = types[i];
					Shipping shipping = dt.runDecision(innerType, weight, area);
					if(shipping.getTotal()>0){
						innerResp+="{\"type\":\""+shipping.getType()+"\",\"area\":\""+shipping.getArea()+"\",\"total\":\""+df.format(shipping.getTotal())+"\"},";
					}
				}
				if(!"".equals(innerResp)){
					innerResp = innerResp.substring(0, innerResp.length()-1);
					jason = "{\"status\":\"1\",\"response\":["+innerResp+"]}";
				}
			}
			//response.getWriter().write(jason);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			response.getWriter().write(jason);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
		try {
			dt = new DecisionTable();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
